package com.link2room.jetfire.controller.api;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.jetfire.dto.param.api.ReservationBaseApiParamDto;
import com.link2room.jetfire.facade.IReservationApiFacadeService;
import com.link2room.jetfire.facade.IReservationFacadeService;
import com.link2room.jetfire.dto.param.base.ReservationParamDto;
import com.link2room.jetfire.dto.result.base.ReservationBaseDto;
import com.link2room.jetfire.dto.result.base.ReservationDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by sss on 2016/9/20.
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/api/Reservations")
public class ReservationApiController {

    @Resource
    private IReservationApiFacadeService reservationApiFacadeService;
    @Resource
    private IReservationFacadeService reservationFacadeService;

    @ApiOperation(value = "获取订单列表",response = ReservationDto.class)
    @RequestMapping(method = RequestMethod.GET)
    public BaseResultDto reservations(
            @RequestParam(value = "lhotelCode" ,required = false) String lhotelCode,
            @RequestParam(value = "resSta" ,required = false) String resSta,
            @RequestParam(value = "confirmSta" ,required = false) String confirmSta
    )throws Exception{
        ReservationBaseApiParamDto reservationBaseApiParamDto=new ReservationBaseApiParamDto();
        reservationBaseApiParamDto.setLhotelCode(lhotelCode);
        reservationBaseApiParamDto.setResSta(resSta);
        reservationBaseApiParamDto.setConfirmSta(confirmSta);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(reservationApiFacadeService.listReservationFroApi(reservationBaseApiParamDto));
        return l2rRs;
    }

    @ApiOperation(value = "根据ID获取订单详情",response = ReservationDto.class)
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public BaseResultDto reservation(
            @PathVariable Long id
    )throws Exception{
        ReservationBaseApiParamDto reservationBaseApiParamDto=new ReservationBaseApiParamDto();
        reservationBaseApiParamDto.setId(id);
        reservationBaseApiParamDto.setNow(new Date());
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(reservationApiFacadeService.getReservationForApi(reservationBaseApiParamDto));
        return l2rRs;
    }

    @ApiOperation(value = "新建订单",response = ReservationBaseDto.class)
    @RequestMapping(method = RequestMethod.POST)
    public BaseResultDto newReservation(
            @RequestBody ReservationParamDto reservationParamDto
    )throws Exception{
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(reservationFacadeService.createReservation(reservationParamDto).getId());
        return l2rRs;
    }

    @ApiOperation(value = "取消订单")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public BaseResultDto cancelReservation(
            @PathVariable Long id
    )throws Exception{
        ReservationBaseApiParamDto reservationBaseApiParamDto=new ReservationBaseApiParamDto();
        reservationBaseApiParamDto.setId(id);
        reservationApiFacadeService.cancelReservationBase(reservationBaseApiParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        return l2rRs;
    }
}
